package com.mall.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.mall.utils.R;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Package: com.mall.exception
 * @author: cxx
 * @Create: 2023/4/16-15:50
 * @Description:
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(NotPermissionException.class)
    public R<String> notpermissionexception(NotPermissionException e) {
        return R.error(e.getMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    public R<String> notLoginException(NotLoginException e) {
        return R.error(" 你还没有登录，请先登录!");
    }

    @ExceptionHandler(NotRoleException.class)
    public R<String> notRoleException(NotRoleException e) {
        return R.error(e.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<String> methodArgumentNotValidException(MethodArgumentNotValidException e) {
        return R.error(e.getFieldErrors().toString());
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public R<String> constraintViolationException(ConstraintViolationException e) {
        return R.error(e.getMessage());
    }

    @ExceptionHandler(NoAddressException.class)
    public R<String> noAddressException(NoAddressException e) {
        return R.error(e.getMessage());
    }
}
